/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author longdm0708l
 */
@Stateless
public class AccountFacade implements AccountFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Account account) {
        em.persist(account);
    }

    public void edit(Account account) {
        em.merge(account);
    }

    public void remove(Account account) {
        em.remove(em.merge(account));
    }

    public Account find(Object id) {
        return em.find(Account.class, id);
    }

    public List<Account> findAll() {
        return em.createQuery("select object(o) from Account as o").getResultList();
    }

    public int checkLogin(String userName, String password) {
        Query q  = em.createQuery("select COUNT(o) from Account as o WHERE o.loginName=:login AND o.password=:password");
        q.setParameter("login", userName);
        q.setParameter("password", password);

        int result = Integer.parseInt(q.getSingleResult().toString());

        return result;
    }

}
